package com.mrobuy.jdvc.dao.impl;

import com.mrobuy.jdvc.Result;
import com.mrobuy.jdvc.dao.VCBrandDao;
import com.mrobuy.jdvc.dao.VCProductCreateDao;
import com.mrobuy.jdvc.entity.VCProductCreateTask;
import com.mrobuy.util.IdentifierGen;
import com.mrobuy.util.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

@Repository("vcProductCreateDao")
public class VCProductCreateDaoImpl implements VCProductCreateDao {

    private static Logger LOG = LoggerFactory.getLogger(VCBrandDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<VCProductCreateTask> list() {

        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_PRODUCT_CREATE_TASK");
        try {
            List<VCProductCreateTask> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(VCProductCreateTask.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public Pagination load(int start, int length, VCProductCreateTask condition) {
        Pagination pagination = new Pagination();

        List<Object> params = new ArrayList<>();

        StringBuilder whereCond = new StringBuilder(" where 1 =1 ");
        if ( condition.getTitle()!=null && condition.getTitle().trim().length() > 0 ) {
            whereCond.append(" and TITLE like ? ");
            params.add("%" + condition.getTitle().trim() + "%");
        }

        if ( condition.getState()!=null && condition.getState().trim().length() > 0 ) {
            whereCond.append("  and STATE = ? ");
            params.add(condition.getState().trim());
        }

        try {

            int count = jdbcTemplate.queryForObject("select count(*) from VC_PRODUCT_CREATE_TASK " + whereCond.toString(), params.toArray(new Object[params.size()]), Integer.class);

            params.add(start);
            params.add(length);
            List<VCProductCreateTask> list = jdbcTemplate.query("select * from VC_PRODUCT_CREATE_TASK " + whereCond.toString() + " limit ?, ?", params.toArray(new Object[params.size()]), new BeanPropertyRowMapper(VCProductCreateTask.class));


            pagination.setRecordsTotal(count);
            pagination.setRecordsFiltered(count);
            pagination.setData(list);
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return pagination;
    }

    @Override
    public Result insert(VCProductCreateTask record) {

        Result result = new Result();

        if ( record==null ||  record.getTitle()==null ||  record.getTitle().trim().length()==0 ) {
            result.setSuccess(false);
            result.setMsg("任务名称不能为空");
            return result;
        }

        try {
            VCProductCreateTask exist =  (VCProductCreateTask)jdbcTemplate.queryForObject("select * from VC_PRODUCT_CREATE_TASK where TITLE = ? ", new Object[]{record.getTitle().trim()},  new BeanPropertyRowMapper(VCProductCreateTask.class));
            if ( exist!=null ) {
                result.setSuccess(false);
                result.setMsg("任务[" + record.getTitle() + "]已经存在, 不能重复创建");
                return result;
            }
        } catch (Exception e) {

        }

        record.setId(IdentifierGen.uuid());
        record.setState("1");
        record.setCreatetime(Calendar.getInstance().getTime());

       int cnt = jdbcTemplate.update("INSERT INTO VC_PRODUCT_CREATE_TASK (ID, TITLE, REMARK, STATE, CREATETIME) VALUES (?, ?, ?, ?, ?)", record.getId(), record.getTitle(), record.getRemark(), record.getState(), record.getCreatetime());

       if ( cnt==0 ) {
           result.setSuccess(false);
           result.setMsg("任务[" + record.getTitle() + "]插入失败");
           return result;
       }

        result.setSuccess(true);
        return result;
    }

    @Override
    public VCProductCreateTask load(String taskid) {

        if ( taskid==null || taskid.trim().length()==0 ) {
            return null;
        }

        try {
            VCProductCreateTask exist =  (VCProductCreateTask)jdbcTemplate.queryForObject("select * from VC_PRODUCT_CREATE_TASK where ID = ? ", new Object[]{taskid.trim()},  new BeanPropertyRowMapper(VCProductCreateTask.class));
            return exist;
        } catch (Exception e) {

        }

        return null;
    }
}
